/*
* @Author: zhanghailin
* @Date:   2017-10-20 13:18:28
* @Last Modified by:   zhanghailin
* @Last Modified time: 2017-10-20 17:58:33
*/
//var clock = null;
//var overviewRefreshTimer = null;

var overview = {

	
	init:function(){
		//overview.setMap();
		//overview.setEchart();
		//页面时钟
		//查询电池详情并赋值
		overview._setDate();
		overviewColckRefreshCount = 0;
		if(overviewColckRefreshTimer){
			window.clearInterval(overviewColckRefreshTimer);
		}
		overviewColckRefreshTimer =window.setInterval(function(){
			overview._setDate();
			//alert("clock");
		}, 6000);
		overview.getOverviewData();
		var cycle = util.getRefreshCycle('REFRESH_CYCLE');
		overviewRefreshCount = 0;
		if(overviewRefreshTimer){
			window.clearInterval(overviewRefreshTimer);
		}
		overviewRefreshTimer = window.setInterval(function(){
			//App.blockUI($("#overview_content"),'努力加载中……');//停止loading
			overview.getOverviewData();
		}, cycle);
		/**if(clock!=null){
			window.clearInterval(clock);
		}
		if(overviewRefreshTimer!=null){
			window.clearInterval(overviewRefreshTimer);
		}
		overview._setDate();
		clock =window.setInterval(function(){
			overview._setDate();
		}, 6000);
		overview.getOverviewData();
		var cycle = util.getRefreshCycle('REFRESH_CYCLE');
		//overviewRefreshCount = 0;
		overviewRefreshTimer = window.setInterval(function(){
			//App.blockUI($("#overview_content"),'努力加载中……');//停止loading
			overview.getOverviewData();
		}, cycle);**/
		
	},

	_setDate:function(){
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth()+1;
		var day = date.getDate();
		var weekend = "星期" + "日一二三四五六".charAt(date.getDay());

		var hours = date.getHours();
		if(hours<10){
			hours="0"+hours;
		}
		var minutes = date.getMinutes();
		if(minutes<10){
			minutes="0"+minutes;
		}
		$("#now").text(year+"-"+month+"-"+day+" "+weekend);

		$("#now_time").text(hours+":"+minutes);

	},

	getOverviewData:function(){
		$.ajax({
			"type" : "post",
			"url" : "monitor/workbench/getOverviewData.mvc",
			"dataType" : "json",
			"async" : true,
			"success" : function(data) {
				$("#todayAlertNum").text(data.todayAlertNum);
				$("#powerOnNum").text(data.powerOnNum);
				//debugger
				//var chart_one = echarts.init(document.getElementById("alertClassifyBarChart"),'macarons');
				//var chart_two = echarts.init(document.getElementById("powerOnDistributeChart"),'macarons');
				//var chart_three = echarts.init(document.getElementById("materialTypeChart"),'macarons');
				//var chart_four = echarts.init(document.getElementById("packModelChart"), 'macarons');
				//var chart_five = echarts.init(document.getElementById("applicationChart"), 'macarons');
				//overview.configBarChart("告警分类", "alertClassifyBarChart", data.alertClassifyBarChartData, chart_one);
				//overview.configMap(data.powerOnDistributeData, chart_two);
				//overview.configBarChart("各正极材料种类电池告警比例","materialTypeChart", data.materialTypeData, chart_three);
				//overview.configPackModelChart(data.packModelData, chart_four);
				//overview.configApplicationAlertChart(data.appticationData, data.appticationDetailData, chart_five);
				//App.unblockUI($("#overview_content"));//停止loading
				
				/*//加载电池上电地图分布
				overview.setMap(data.powerOnDistributeData);
				//加载电池各系统型号告警比例
				overview.configPackModelChart(data.packModelData);

				overview.configApplicationAlertChart(data.appticationData, data.appticationDetailData);
				//console.log(data);

				overview.configBarChart("告警分类","chart-1",data.alertClassifyBarChartData);
				overview.configBarChart2("各正极材料种类电池告警比例","chart-2",data.materialTypeData);*/
			},
			error : function(e) {
				//出错
				rmsBox.notify('error', {msg: '出错了！', delay:3000});
				//App.unblockUI($("#overview_content"));//停止loading
				}
			});
		
		$.ajax({
			"type" : "post",
			"url" : "monitor/workbench/getPowerOnDistributeData.mvc",
			"dataType" : "json",
			"async" : true,
			"success" : function(data) {
				//加载电池上电地图分布
				overview.setMap(data.powerOnDistributeData);
			},
			error : function(e) {
				//出错
				rmsBox.notify('error', {msg: '出错了！', delay:3000});
				//App.unblockUI($("#overview_content"));//停止loading
				}
			});
		
		$.ajax({
			"type" : "post",
			"url" : "monitor/workbench/getPackModelData.mvc",
			"dataType" : "json",
			"async" : true,
			"success" : function(data) {
				//加载电池各系统型号告警比例
				overview.configPackModelChart(data.packModelData);
			},
			error : function(e) {
				//出错
				rmsBox.notify('error', {msg: '出错了！', delay:3000});
				//App.unblockUI($("#overview_content"));//停止loading
				}
			});
		
		$.ajax({
			"type" : "post",
			"url" : "monitor/workbench/getAppticationData.mvc",
			"dataType" : "json",
			"async" : true,
			"success" : function(data) {
				//加载电池各系统型号告警比例
				overview.configApplicationAlertChart(data.appticationData, data.appticationDetailData);
			},
			error : function(e) {
				//出错
				rmsBox.notify('error', {msg: '出错了！', delay:3000});
				//App.unblockUI($("#overview_content"));//停止loading
				}
			});
		
		$.ajax({
			"type" : "post",
			"url" : "monitor/workbench/getAlertClassifyBarChartData.mvc",
			"dataType" : "json",
			"async" : true,
			"success" : function(data) {
				overview.configBarChart("告警分类","chart-1",data.alertClassifyBarChartData);
			},
			error : function(e) {
				//出错
				rmsBox.notify('error', {msg: '出错了！', delay:3000});
				//App.unblockUI($("#overview_content"));//停止loading
				}
			});
		
		$.ajax({
			"type" : "post",
			"url" : "monitor/workbench/getMaterialTypeData.mvc",
			"dataType" : "json",
			"async" : true,
			"success" : function(data) {
				overview.configBarChart("各正极材料种类电池告警比例","chart-2",data.materialTypeData);
			},
			error : function(e) {
				//出错
				rmsBox.notify('error', {msg: '出错了！', delay:3000});
				//App.unblockUI($("#overview_content"));//停止loading
				}
			});
		},

	//加载动画
	showLoad:function(id){
		id.showLoading({
		    text: '正在努力的读取数据中...',    //loading
		});		
	},
	setMap:function(data){
	    var myChart = echarts.init(document.getElementById('battery-map'));
		//加载动画
		overview.showLoad(myChart);
        myChart.setOption({      
		    title : {
		        text: '电池分布',
		        subtext: '',
		        x:'center',
		        y:'150',
		        textStyle:{
		        	color: '#FFF'
		        }
		    },
		    tooltip : {
		        trigger: 'item'
		    },
		    legend: {
		        orient: 'vertical',
		        x:'left',
		        data:[''],
		        textStyle:{
		        	color: '#FFF'
		        }
		    },
		    dataRange: {
		        min: 0,
		        max: 2500,
		        x: 'left',
		        y: 'bottom',
		        text:['高','低'],
		        textStyle:{
		        	color: '#FFF'
		        },
		        // 文本，默认为数值文本
		        calculable : true
		    },
		    toolbox: {
		        show: false
		    },
		    roamController: {
		        show: false
		    },
		    series : [
		        {
		            name: '电池',
		            type: 'map',
		            mapType: 'china',
		            roam: false,
		            itemStyle:{
		                normal:{label:{show:true}},
		                emphasis:{label:{show:true}}
		            },
		            data:data
		        }
		    ]
        })
    	myChart.hideLoading();	//loading hidden
	},

	configPackModelChart:function(data){
		//debugger
		var myChart = echarts.init(document.getElementById('chart-3'));
		var dataX = new Array();
		var dataY = new Array();
		for (var index = 0; index < data.length; index++) {
			dataX.push(data[index]['name']);
			dataY.push(data[index]['value']);
		}
		//console.log(dataX);
		//console.log(dataY);
		myChart.setOption({
		    tooltip : {
		        trigger: 'item',
		        formatter: "{a} <br/>{b} : {c} ({d}%)"
		    },
		    legend: {
		        orient : 'vertical',
		        x : 'left',
		        data:dataX,
		        textStyle:{
		        	color: '#FFF'
		        }
		    },
		    toolbox: {
		        show : true,
		        feature : {
		            mark : {show: false},
		            dataView : {show: false, readOnly: false},
		            magicType : {
		                show: false, 
		                type: ['pie', 'funnel'],
		                option: {
		                    funnel: {
		                        x: '25%',
		                        width: '50%',
		                        funnelAlign: 'center',
		                        max: 1548
		                    }
		                }
		            },
		            restore : {show: false},
		            saveAsImage : {show: false}
		        }
		    },
		    calculable : true,
		    series : [
		        {
		            name:'告警比例',
		            type:'pie',
		            radius : ['50%', '70%'],
		            itemStyle : {
		                normal : {
		                    label : {
		                        show : false
		                    },
		                    labelLine : {
		                        show : false
		                    }
		                },
		                emphasis : {
		                    label : {
		                        show : true,
		                        position : 'center',
		                        textStyle : {
		                            fontSize : '16',
		                            fontWeight : 'bold'
		                        }
		                    }
		                }
		            },
		            data:data
		        }
		    ]
		})
	},

	/**配置应用方向告警比例 chart
	 * @param dataInside 内环数据
	 * @param dataOutside 外环数据
	 */
	configApplicationAlertChart : function(dataInside, dataOutside) {
		if(dataInside.length==0&&dataInside.length==0){
			return
		}
		var myChart = echarts.init(document.getElementById('chart-4'));
		myChart.setOption({
			title : {
				text : '应用方向告警比例',
				x:'center',
				y:'top',
				textStyle:{
					fontSize:12,
					color:'#FFFFFF'
				}
			},
		    tooltip : {
		        trigger: 'item',
		        formatter: "{a} <br/>{b} : {c} ({d}%)"
		    },
		    legend: {
		        orient : 'vertical',
		        x : 'left',
		        data:dataInside,
		        textStyle:{
		        	color: '#FFF'
		        }
		    },
		    toolbox: {
		        show : false,
		        feature : {
		            mark : {show: false},
		            dataView : {show: false, readOnly: false},
		            magicType : {
		                show: false, 
		                type: ['pie', 'funnel']
		            },
		            restore : {show: false},
		            saveAsImage : {show: false}
		        }
		    },
		    calculable : false,
		    series : [
		        {
		            name:'告警比例',
		            type:'pie',
		            selectedMode: 'single',
		            radius : [0, 50],
		            
		            // for funnel
		            x: '20%',
		            width: '40%',
		            funnelAlign: 'right',
		            max: 1548,
		            
		            itemStyle : {
		                normal : {
		                    label : {
		                        position : 'inner'
		                    },
		                    labelLine : {
		                        show : false
		                    }
		                }
		            },
		            data:dataOutside
		        },
		        {
		            name:'告警比例',
		            type:'pie',
		            radius : [80, 110],
		            
		            // for funnel
		            x: '60%',
		            width: '35%',
		            funnelAlign: 'left',
		            max: 1048,
		            data:dataOutside
		        }
		    ]
		})
	},

	configBarChart:function(title, div_id, data){
		//debugger
		console.log(data.length);
		if(data==null||data.length==0) {
			return
		}
		var dataX = new Array();
		var dataY = new Array();
		var legend_data = [title];
		for (var index = 0; index < data.length; index++) {
			dataX.push(data[index]['x']);
			dataY.push(data[index]['y']);
		}
	    var myChart = echarts.init(document.getElementById(div_id));
		// 柱形图
		function setData(id){
			//加载动画
			overview.showLoad(id);
	        id.setOption({
	        	backgroundColor:'rgba(255,255,255,.1)',
	            tooltip: {
	                show: true
	            },
	            legend: {
	                data:legend_data,
	                textStyle:{
			        	color: '#FFF'
			        }
	            },
	            xAxis : [
	                {
	                    type : 'category',
	                    data : dataX,
			          	axisLabel:{
			          		show: true,
			          		textStyle:{
			          			color: '#FFF'
			          		}
			          	}
	                }
	            ],
	            yAxis : [
	                {
	                    type : 'value',
			          	axisLabel:{
			          		show: true,
			          		textStyle:{
			          			color: '#FFF'
			          		}
			          	}
	                }
	            ],
	            series : [
	                {
	                    "name":"告警分类",
	                    "type":"bar",
	                    "barWidth" : 10,
	                    "data":dataY
	                }
	            ]
	        },true)
	    	id.hideLoading();	//loading hidden
		}
		setData(myChart);
	}
}
